package com.example.javapicalc;
import org.javapicalc.*;

class Transmitter extends Method<String, String>{
	//@Override
	public boolean connectedToCar = false;
	public void run(final Channel<String, String> talk1_, final Channel<String, String> switch1_,
			final Channel<Channel<String, String>, Channel<String, String> > lose1_, 
			Channel<Channel<String, String>, Channel<String, String>> gain1_){
		// if msg comes over lose1 then transfer talk1_ to idtrans
		Thread talkThread = new Thread(){
			public void run(){
				String msg1 = null, msg2 = null;
				talk1_.receive(msg1, msg2);
				System.out.println(this.toString()+" received "+msg1+" and "+msg2+" from car");
			}
		};
		Thread transferThread = new Thread(){
			public void run(){
				Channel<String, String> talk2_ = null;
				Channel<String, String> switch2_ = null;
				Receive(lose1_, talk2_, switch2_);
				Send(switch1_, "NOTHING", "Switch to new trans");		
				System.out.println(this.toString()+" Sent msg to switch");
			}
		};
		talkThread.start();
		transferThread.start();
	}
	
}
class CarObj extends Method<String, String>{
	public void run(final Channel<String, String> talk_, final Channel<String, String> switch_, 
			 final Channel<String, String> talk2_, Channel<String, String> switch2_){
		boolean trans = true;
		Thread talkThread = new Thread(){
			public void run(boolean trans){
				if(trans) Send(talk_, "hi", "there");
				else Send(talk2_, "hi", "there");
			}
		};
		Thread switchThread = new Thread(){
			public void run(){
				String msg1=null, msg2=null;
				Receive(switch_, msg1, msg2);
				//	if(msg2 == "Switch to new trans")
				System.out.println("received msg "+msg1 + "  and "+msg2+" from switch_");				
			}
		};
		talkThread.start();
		switchThread.start();
	}
}
class ControlObj extends Method<String, String>{
	public void run(Channel lose1_, Channel gain1_, Channel lose2_, Channel gain2_){
		Channel talk2_ = new Channel();
		Channel switch2_ = new Channel();
		Transmitter trans = (Transmitter) super.Connections.get(lose1_); 
		Transmitter idtrans = (Transmitter) super.Connections.get(lose2_);
		if(trans.connectedToCar){
			Send(lose1_, talk2_, switch2_);
			Send(gain2_, talk2_, switch2_);
		}
		else{
			Send(lose2_, talk2_, switch2_);
			Send(gain1_, talk2_, switch2_);
		}
	}
}
public class CarTrans{
	public static void main(String s[]){
		Channel talk1_= new Channel(), switch1_= new Channel(), lose1_ = new Channel(), gain1_ = new Channel(), lose2_ = new Channel(), gain2_ = new Channel();
		ControlObj ctrl = new ControlObj();
		Transmitter trans = new Transmitter(), idtrans = new Transmitter();
		CarObj car = new CarObj();
		ctrl.addConnection(lose1_, trans);
		trans.addConnection(lose1_, ctrl);
		ctrl.addConnection(lose2_, idtrans);
		idtrans.addConnection(lose2_, ctrl);
		ctrl.addConnection(gain1_, trans);
		trans.addConnection(gain1_, ctrl);
		ctrl.addConnection(gain2_, idtrans);
		idtrans.addConnection(gain2_, ctrl);
		car.addConnection(talk1_, trans);
		trans.addConnection(talk1_, car);
		car.addConnection(switch1_, trans);
		car.addConnection(switch1_, car);
		trans.connectedToCar = true;
		/// Initial set up done
	}
}